﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace HuaJuanAudioNevolDown
{
    public partial class frmAbout : Form
    {
        string versionFileUrl = "https://gitee.com/msmvc/HuaJuanAudioNevolDown/raw/master/src/version.json";
        VersionInfo versionInfo = null;

        public frmAbout()
        {
            InitializeComponent();
        }

        private void frmAbout_Load(object sender, EventArgs e)
        {
            VersionInfo versionInfo = Service.Instance.GetLocalVersion(Application.StartupPath + "\\version.json");
            lblCurrentVersion.Text = versionInfo.Version;
            btnDownNewVersion.Enabled = false;
        }

        private async void btnCheckUpgrade_Click(object sender, EventArgs e)
        {
            versionInfo = await Service.Instance.GetRemoteVersionAsync(versionFileUrl);
            if (versionInfo != null)
            {
                lblLastestVersion.Text = versionInfo.Version;
                var cmp = string.Compare(lblCurrentVersion.Text, versionInfo.Version);
                switch (cmp)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case -1:
                        btnDownNewVersion.Enabled = true;
                        break;
                }
            }
            else
            {
                MessageBox.Show("未检测到有更新，请到发布网站查看", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnDownNewVersion_Click(object sender, EventArgs e)
        {
            if (versionInfo != null)
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = "https://gitee.com/msmvc/HuaJuanAudioNevolDown/releases/",
                    UseShellExecute = true
                });
            }
        }

        private void lnkSource_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var url = "https://gitee.com/msmvc/HuaJuanAudioNevolDown";
            Process.Start(new ProcessStartInfo
            {
                FileName = url,
                UseShellExecute = true
            });
        }

        private void lnkDown_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var url = "https://gitee.com/msmvc/HuaJuanAudioNevolDown/releases/";
            Process.Start(new ProcessStartInfo
            {
                FileName = url,
                UseShellExecute = true
            });
        }
    }
}
